package com.hxd.otc.main;

import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

import com.hxd.otc.MainActivity;
import com.hxd.otc.R;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.base.BaseApplication;
import com.hxd.otc.event.Event;
import com.hxd.otc.main.contract.WelcomeContract;
import com.hxd.otc.main.persenter.WelcomePersenter;
import com.hxd.otc.utils.CommonUtil;
import com.hxd.otc.utils.hyphenate.HyphenateKeFuUtil;
import com.hxd.otc.utils.rongcloud.RongCloudUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;

/**
 * 作  者 :  胡庆岭
 * 时  间 :  2018/6/20 16:10
 * 描  述 :  ${启动页}
 */
public class WelcomeActivity extends BaseActivity<WelcomePersenter> implements WelcomeContract.View {
    @BindView(R.id.welcome_iv)
    ImageView mWelcomeImage;
    private static final int WHAT = 5;
    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == WHAT) {
//                if (SpUtil.PERMANENT.getInt(SpConfig.Welcome.GUIDE_VERSION, 0) < CommonUtil.getVersionCode()) {
//                    $startActivity(GuideActivity.class, true);
//                } else {
                    $startActivity(MainActivity.class, true);
//                }
            }
            return true;
        }
    });


    @Override
    protected int getLayoutId() {
        return R.layout.activity_welcome;
    }

    @Override
    protected void initView() {
        mWelcomeImage.setImageResource(R.mipmap.icon_welcome);
    }

    @Override
    protected void initData() {
        registerEventBus();
        RongCloudUtils.getInstance().connectRongCloud();  //重新登录时，连接融云
        HyphenateKeFuUtil.getInstance().register(); //连接环信客服
    }

    private void initGuide() {
        mHandler.sendEmptyMessageDelayed(WHAT, 1);
    }

    @Override
    protected void initEvent() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Event.StartActivityEvent event) {
        if (event != null) {
            initGuide();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        pingIP();

    }

    private void pingIP() {

        //如果不是正式环境，则不PING  域名
        if (verificationUrl()) return;
        mPresenter.doDing(true);

//
    }

    private boolean verificationUrl() {

        if (CommonUtil.verificationUrl(BaseApplication.SERVER_HOST)) {
            mHandler.sendEmptyMessageDelayed(WHAT, 1000);
            return true;
        }
        return false;
    }

    @Override
    protected WelcomePersenter createPresenter() {
        return new WelcomePersenter(this);
    }

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            mHandler.removeMessages(WHAT);
            mHandler = null;
        }
        super.onDestroy();
        unregisterEventBus();
    }
}
